import java.util.ArrayList;
import java.util.List;

//无头双向链表实现
public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        } //构造方法用于给节点中设置值
    }

    public ListNode head; //标记双向链表的头部
    public ListNode tail; //标记双向链表的尾部

    // 1 打印双向链表数据，默认从头开始打印
    public void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    // 2 得到双向链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    // 3 查找是否包含关键字key是否在双向链表中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 4 头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
           this.head = node;
           this.tail = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
        //head的前驱没必要置空，因为new的时候默认就是空的了
    }

    // 5 尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.tail = node;
        }
        this.tail.next = node;
        node.prev = this.tail;
        this.tail = node;
    }

    // 6 任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data){
        //1.判断index位置的合法性
        if(index < 0 || index > size()){
            throw new IndexWrongfunException("index位置不合法");
        }
        //2.考虑特殊位置：头插和尾插
        if(index == 0){
            addFirst(index);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        //3.找到index位置节点的地址
        ListNode cur = findIndex(index);
        //4.修改4个指向
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private ListNode findIndex(int index){
        ListNode cur = this.head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    // 7 删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head;
        while (cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){ //此时并不只有一个节点
                        head.prev = null;
                    }else { //这里表示只有一个节点，此时head已经是null了，只需要再吧tail也置为null
                        tail = null;
                    }
                }else {
                    /* 判断两种情况：（用cur.next == null判断，等于null就是tail）
                    1.cur是tail，将cur.prev.next = cur.next = null;然后将tail往前移动
                    2.cur不是尾节点，执行cur.prev.next = cur.next;
                                      cur.next.prev = cur.prev;
                     */
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        this.tail = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
                return; //找到之后就return，不用再执行后续的
            }else {
                cur = cur.next;
            }
        }
    }

    // 8 删除所有值为key的结点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while (cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){ //此时并不只有一个节点
                        head.prev = null;
                    }else { //这里表示只有一个节点，此时head已经是null了，只需要再吧tail也置为null
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        this.tail = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    // 9 清空双向链表
    public void clear(){
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        //原始的头节点和尾节点都要置空
        this.head = null;
        this.tail = null;
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(1);
        myLinkedList.addLast(1);
        myLinkedList.addLast(2);
        myLinkedList.addLast(3);
        myLinkedList.addLast(1);
        myLinkedList.display();


        /*myLinkedList.addIndex(3,99);
        myLinkedList.display();*/

        myLinkedList.remove(1);
        myLinkedList.display();

        myLinkedList.removeAllKey(1);
        myLinkedList.display();

        myLinkedList.clear();
        myLinkedList.display();
    }
    /*//ArrayList的使用
    public static void main(String[] args) {
        //构造一个空的LinkedList
        List<Integer> list1 = new ArrayList<>();
        List<String> list2 = new java.util.ArrayList<>();

        list2.add("JavaSE");
        list2.add("JavaWeb");
        list2.add("JavaEE");
        //使用ArrayList构造LinkedList
        List<String> list3 = new ArrayList<>(list2);
    }*/
}
